package org.rbac.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.rbac.vo.Role;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Controller
@RequestMapping(value = "/upload")
public class UploadAction {

	@RequestMapping(value = "/single")
	@ResponseBody
	public void upload(HttpServletRequest request, HttpServletResponse response, Role r){
		// 转型为MultipartHttpRequest：
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		// 获得文件：
        MultipartFile file = multipartRequest.getFile("file");   
        // 获得文件名：
        String filename = file.getOriginalFilename();  
        InputStream input = null;
        OutputStream ops = null;
        // 获得输入流：
        try {
        	input = file.getInputStream();
        	ops = new FileOutputStream(new File("D:/" + filename));
        	byte[] tmp = new byte[1024];
        	int lenght = -1;
        	while ( (lenght = input.read(tmp)) != -1) {
        		ops.write(tmp, 0, lenght);
        	}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != ops) {
					ops.close();
				}
				if (null != input) {
					input.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
        // 写入文件   
  
	}
}
